package com.scl.springtalis.controller;

import com.scl.springtalis.polo.Emp;
import com.scl.springtalis.polo.Result;
import com.scl.springtalis.service.EmpService;
import com.scl.springtalis.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpService empService;
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        Emp login = empService.login(emp);

        if (login != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", login.getId());
            map.put("name", login.getName());
            map.put("username", login.getUsername());
            String s = JwtUtils.generateJwt(map);
            return Result.success(s);
        }
        return Result.error("密码错误");
    }
}
